/*
 *
 * Created on: 07.01.2013
 *
 * AcknowledgementParser.cpp
 * This file is part of KOBUS
 * 
 * Copyright (C) 2012 - Ivan Penkin, MIPT
 * grek.penkin@gmail.com
 *
 * KOBUS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * KOBUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KOBUS; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 *  
 */


#include <DHAcknowledgementParser.h>

using namespace kobus;
using namespace dhcp;

void DHAcknowledgementParser::initializeByConfig() throw (Exception) {

  setRegex(
      config.getString(DHConfig::Keys::ackPraser::ackRegex));
  setIfaceGrpNum(
      config.getInt(DHConfig::Keys::ackPraser::ifaceGrp));
  setVlanGrpNum(
      config.getInt(DHConfig::Keys::ackPraser::vlanGrp));
  setMacGrpNum(
      config.getInt(DHConfig::Keys::ackPraser::macGrp));
  setIpGrpNum(
      config.getInt(DHConfig::Keys::ackPraser::ipGrp));
  setRemoteIdGrpNum(
      config.getInt(DHConfig::Keys::ackPraser::remoteIdGrp));
  setCircuitIdGrpNum(
      config.getInt(DHConfig::Keys::ackPraser::circuitIdGrp));
}


HostInfo DHAcknowledgementParser::getNewHostInfo() throw (Exception) {

  HostInfo info = getNewHostInfoWithoutFlags();
  info.setFlags(dhcp::Acknowledgement);

  return info;
}

void DHAcknowledgementParser::check() const throw (Exception) {

  if (bufers.hostInfo[Bufers::IFACE].isEmpty()) {
    throw Exception(Exception::NO_ERRNO,
      "AcknowledgementParser::check(): Non-optional group 'iface' is empty. "
      "In message:"
        + bufers.message);
  }

  //TODO: check MAC
  if (bufers.hostInfo[Bufers::MAC].isEmpty()) {
    throw Exception(Exception::NO_ERRNO,
      "AcknowledgementParser::check(): Non-optional group 'mac' is empty. "
      "In message:"
        + bufers.message);
  }

  QHostAddress ip(bufers.hostInfo[Bufers::IP]);
  if (ip.isNull()) {
    throw Exception(Exception::NO_ERRNO,
       "AcknowledgementParser::check(): Non-optional group 'ip' is empty. "
       "In message:"
        + bufers.message);
  }

  if (parsingParms.vlanGrpNum > DHConfig::MIN_OPTIONAL_GROUP_NUM) {
    bool conversionResult = false;
    quint16 vlan = bufers.hostInfo[Bufers::VLAN].toUInt(&conversionResult);
    if (!conversionResult || vlan == 0) {
      throw Exception(Exception::NO_ERRNO,
          "AcknowledgementParser::check(): Optional group 'vlan' is empty. "
          "In message:"
            + bufers.message);
    };
  }

  if (parsingParms.remoteIdGrpNum > DHConfig::MIN_OPTIONAL_GROUP_NUM) {
    if (bufers.hostInfo[Bufers::REMOTE_ID].isEmpty()) {
      throw Exception(Exception::NO_ERRNO,
          "AcknowledgementParser::check(): Optional group 'remote_id' is empty."
          " In message:"
            + bufers.message);
    };
  }

  if (parsingParms.circuitIdGrpNum > DHConfig::MIN_OPTIONAL_GROUP_NUM) {
    if (bufers.hostInfo[Bufers::CIRCUIT_ID].isEmpty()) {
      throw Exception(Exception::NO_ERRNO,
          "AcknowledgementParser::check(): Optional group 'circuit-id' is empty."
          " In message:"
          + bufers.message);
    };
  }
}

